8.Media and Camera/Media Playback

1.Media Palyback

  The Android multimedia framework includes support for playing variety of common media types, so that you can easily integrate audio, video

    and images into your applications.

  You can play audio or video from media files stored in your application‘s resources (raw resources), from standalone files in the filesystem,

    or from a data stream arriving over a network connection, all using MediaPlayer APIs.

2. The Basic

  MediaPlayer ---> Primary API for Playing sound and video

  AudioManager ---> managers audio sources and audio output on a device

3. Manifest Declarations

  <uses-permission android:name="android.permission.INTERNET" />

  <uses-permission android:name="android.permission.WAKE_LOCK" /> : keep the screen from dimming or the processor from sleeping

4. Using MediaPalyer

  An object of this class can fetch, decode, and play both audio and video with minimal setup. It supports several different media sources

    such as:

    <1>Local resources

      Here is an simple example of how to play audio

      <i> place a local resources saved in res/raw/ directory

      <ii>

MediaPlayer mediaPlayer = MediaPlayer.create(getApplication(),R.raw.beatiful_girl);
        mediaPlayer.start();

    <2>Internal URIs, such as one you might obtain from a Content Resolver

Uri myUri = ....; // initialize Uri here, obtained through Content Resolver
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();

    <3>External URLs(streaming)

String url = "http://........"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();

  4.1 Asynchronous Preparation

    important to keep in mind:

    <1> the call to prepare() can take a long time to excute, because it might involve fetching and decoding media data.

         So, never call it from application`s UI thread.

    <2> framework supplies a convinient way to accomplish this task by using prepareAsync() method

       When the media is done preparing, the onPrepared() method of the MediaPlayer.OnPreparedListener, configured through

        setOnPreparedListener() is called.

  4.2 Managing State

    

  4.3 Releasing the MediaPlayer

    A MediaPlayer can consume valuable system resources. Therefore, you should call release() to make sure any  system resources

      allocated toit are properly released 

mediaPlayer.release();
mediaPlayer = null;

    As an example, consider the problems that could happen if you forgot to release the MediaPlayer when your activity is stopped, but

      create a new one when the activity starts again. As you may know, when the user changes the screen orientation (or changes the

      device configuration in another way), the system handles that by restarting the activity (by default), so you might quickly consume

      all of the system resources as the user rotates the device back and forth between portrait and landscape, because at each

      orientation change, you create a new MediaPlayer that you never release.

5. Using a Service with MediaPlayer

  If you want your media to play in the background even when your application is not onscreen—that is, you want it to continue playing

    while the user is interacting with other applications—then you must start a Service and control the MediaPlayer instance from there.

  5.1 Running asynchronousyly

    First of all, like an Activity, all work in a Service is done in a single thread by default.

    if you are running an activity and a service from the same application("main thread") by default,

    Therefore, services needs to process incoming intents quickly and never perform lengthly computatipons when responding to them.

    If any heavy work or blocking calls are expected, you must do those tasks asynchronously:

      either from another thread you implement yourself, or using the framework‘s many facilities for asynchronous processing.

时间: 2024-10-06 20:09:55

8.Media and Camera/Media Playback的相关文章

media type和media query

media type media type是CSS2的重要属性,通过它,可以针对不同的设备指定不同的样式. media type种类: 用法: <link href="style.css" media="screen print" ... @media screen{ selector{rules} } 例子: <style type="text/css"> @media screen{ body{ background: gr

Android官方Media Playback中文教程

此文参照 android developer API Guides https://developer.android.com/guide/topics/media/mediaplayer.html Media Playback(媒体播放) Android 多媒体框架能够支持多种普通媒体类型,所以我们很容易的整合音频,视频,图片到我们的应用中来.这些多媒体的资源可以是本地文件系统上的,也可以是网络上的.在android中播放音频视频使用的都是MediaPlayer apis. 这篇文档会向我们展

因为Android M权限问题导致的&quot;Permission Denial: reading com.android.providers.media.MediaProvider&quot;解决办法

程序出错报告 在模拟器上调试程序,出错代码如下: Cursor cur = context.getContentResolver().query( MediaStore.Audio.Media.INTERNAL_CONTENT_URI, new String[] { MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media

[Winform]Media Player播放控制面板控制,单击事件截获

摘要 在项目中有这样的一个需求,需要在一台宣传机器上循环播放一段视频,并在体验的用户单击鼠标左键的时候推出全屏,可以让用户体验电脑的其它功能. 解决方案 考虑到都是windows系统的,所以采用了windows自带的播放器,Windows Media Player Com组件作为视频播放器. 如图 但是,为了更好满足需求,下面的播放控制面板需要隐藏,该怎么做呢?最后找到解决方案,设置播放器属性 windowsMediaPlay.uiMode = "none"; 循环播放的代码,这里为播

[Winform]Media Player组件全屏播放的设置

摘要 在设置程序开始运行时,让视频全屏播放时,直接设置 windowsMediaPlay.fullScreen = true; 会报错,代码如下 windowsMediaPlay.URL = _videoPath; windowsMediaPlay.Ctlcontrols.play(); windowsMediaPlay.fullScreen = true; windowsMediaPlay.ClickEvent += windowsMediaPlay_ClickEvent; windowsMe

windows media player 播放视频

一.新建windows应用程序项目,添加vedioForm窗体 二.在com组件中找到windows media player,添加引用 三.代码如下: 1 public partial class VedioForm : Form 2 { 3 private AxWMPLib.AxWindowsMediaPlayer axWindowsMediaPlayer1; 4 public VedioForm() 5 { 6 InitializeComponent(); 7 InitVedio(); 8

winform播放视频(windows media player)

1.找到windows media player 工具箱常规下边右键,右键弹窗点击“选择项”,选择工具箱窗口点击“COM组件”,找到 Windows Media Player 勾选,点击确定 2.使用windows media player 直接将windows media player控件拖动到窗体上, 3.属性 (1)fullScreen:满屏 (2)enableContextMenu:是否显示右键菜单 (3)stretchToFit :非全屏状态时是否伸展到最佳大小 (4)uMode: 播

使用media来加载css

默认的,css被当做渲染时候必须加载的资源. 设备类型和设备询问允许我们设置一些css资源编程可选的 对于所有的css资源,无论是必须的还是可选的,都会被浏览器加载 The New York Times with CSSThe New York Times without CSS (FOUC) 上面显示了加载css和不加载css样式的效果,不加载css显示的页面同样被叫做 “Flash of Unstyled Content” (FOUC). 所有,页面的渲染必须需要DOM和CSSOM. css

CSS3学习教程:Media Queries详解

说起CSS3的新特性,就不得不提到 Media Queries . Media Queries 的引入,其作用就是允许添加表达式用以确定媒体的情况,以此来应用不同的样式表.换句话说,其允许我们在不改变内容的情况下,改变页面的布局以精确适应不同的设备,以此加强体验.所以Media Queries和CSS优化没有关系,甚至是矛盾的. CSS2.1定义了 Media 的部分,包括类型.组别和规则等.CSS并非为了显示器而创造,而是应用于各种各样的媒体,比如常见的显示器,越来愈多的手持设备,可能略显过时