一起学Android之音频视频

移动开发 作者: 2024-08-24 14:50:01
本文演示了如何通过Api播放音频和视频文件,来获得良好性能和愉快的用户体验,仅供学习分享使用,如有不足之处,还请指正。
  1.  MediaPlayer 可以用来控制audio/video文件或流播放的类。通过此类,可以方便的控制音频/视频文件的播放,暂停和停止等操作。
  2. Uri 统一资源标识符(Uniform Resource Identifier,或URI)是一个用于标识某一互联网资源名称的字符串。
  3. VideoView 自带的一种播放视频的组件。
  4. SurfaceView 可以通过后台绘制显示的视图组件。 
 1 private MediaPlayer mMediaPlayer;
 2 
 3     /**
 4      * 开始
 5      * @param v
 6      */
 7     public void bn_start(View v){
 8         if(mMediaPlayer==null) {
 9             ready();
10         }
11         mMediaPlayer.start();
12     }
13 
14  15      * 创建MediaPlayer并到prepare状态
16      17     private  ready(){
18         ){
19             mMediaPlayer=new MediaPlayer();//Idle
20             Log.i("TAG","ready: "+filePath);
21             File file=new File(Environment.getExternalStorageDirectory(),"goldfallen.mp3");
22             Log.i("TAG","ready: "+file.getPath());
23             Log.i("TAG",1)">file.exists());
24             Uri uri= Uri.fromFile(file);
25             try {
26                 mMediaPlayer.setDataSource(AudioActivity.this,uri);
27                 mMediaPlayer.prepare();Prepared
28             } catch (IOException e) {
29                 Log.i("TAG",1)"> e.getMessage());
30                 e.printStackTrace();
31             }
32 33     }
1 2      * 暂停
3 4      5      bn_pause(View v){
6         if(mMediaPlayer!=null && mMediaPlayer.isPlaying()){
7             mMediaPlayer.pause();
8 9     }
 1    2      * 停止
 3  4       5      bn_stop(View v){
 6          7             mMediaPlayer.stop();
 8             mMediaPlayer.release();
 9             mMediaPlayer=;
11     }
 @Override
2     protected  onDestroy() {
3         4 5 6             mMediaPlayer=8         super.onDestroy();
9     }
     * D:\Android\Project\DemoMedia\app\src\main\res\raw\A2012.mp3:
     * Error: 'A' is not a valid file-based resource name character:
     * File-based resource names must contain only lowercase a-z,0-9,or underscore
 5       6      ready2(){
 7          8             mMediaPlayer= MediaPlayer.create(10     }
class AudioService extends Service {
 4 
 OperatorReceiver mReceiver;
 6 
public AudioService() {
 9 
    @Override
11      onCreate() {
12         .onCreate();
13         mMediaPlayer=new MediaPlayer();
14         mReceiver= OperatorReceiver();
15         IntentFilter filter=new IntentFilter("operator.receiver"16         registerReceiver(mReceiver,filter);
17 18 
19 20      IBinder onBind(Intent intent) {
21         return 22 23 
24 25     int onStartCommand(Intent intent,int flags,1)">int startId) {
26         Log.i("TAG","onStartCommand: ----ready "27         ready();
28         .onStartCommand(intent,flags,startId);
29 30 
32     33         34 35 36         37 38             mMediaPlayer=39 40         unregisterReceiver(mReceiver);
41         42 43 
44      start(){
45 46 47 
48     private   pause(){
49         mMediaPlayer.pause();
50 51 
52      stop(){
53         mMediaPlayer.stop();
54 55 56 
57     58         mMediaPlayer.reset();
59         60             String filePath= Environment.getExternalStorageDirectory()+"/ab2012.mp3"61             Log.i("TAG",1)">filePath);
62             File file= File(filePath);
63             if(file.exists()) {
64                 mMediaPlayer.setDataSource(filePath);
65                 mMediaPlayer.prepare();
66             }else{
67                 Log.i("TAG","ready: 文件不存在 "68 69         } 70             Log.i("TAG",1)">e.getMessage());
71             e.printStackTrace();
72 73 74 }
 1       * 操作接收器
 3       4     class OperatorReceiver  BroadcastReceiver
    {
 6         @Override
 onReceive(Context context,Intent intent) {
 8             int cmd=intent.getIntExtra("cmd",-1 9             Log.i("TAG","onReceive: "+cmd);
10             switch (cmd){
11                 case Tools.PLAY:
                    start();
13                     break14                  Tools.PAUSE:
                    pause();
16                     17                  Tools.STOP:
18                     stop();
19                     20                 default:
21 22                     25 26     }
1   Intent intent =new Intent(this,AudioService.class2   startService(intent);
 6         Intent intent=new Intent("operator.receiver" 7         intent.putExtra("cmd"        sendBroadcast(intent);
 9         Log.i("TAG","bn_start: "+"service"11 
12     13 14 15      16     17         Intent intent=18         intent.putExtra("cmd"20 21 
22     23 25      26     27         Intent intent=28         intent.putExtra("cmd"30     }
1  <service
2             android:name=".AudioService"
3             android:enabled="true"
4             android:exported="true">
5         </service>
 VideoView mVideoView;
 onCreate(Bundle savedInstanceState) {
 4         .onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
 6         mVideoView= (VideoView) .findViewById(R.id.vv_info);
设置路径
 8         File file=10         if(!file.exists()){
11             Log.i("TAG","onCreate: 文件不存在"13         Uri uri=Uri.fromFile(file);
        mVideoView.setVideoURI(uri);
15         设置Media Controller
16         MediaController controller=new MediaController(        mVideoView.setMediaController(controller);
获取焦点
        mVideoView.requestFocus();
        mVideoView.start();
设置播放完成事件
22         mVideoView.setOnCompletionListener( MediaPlayer.OnCompletionListener() {
            @Override
24              onCompletion(MediaPlayer mp) {
25                 Toast.makeText(VideoActivity.26         });
28     }
public   bn_start(View view)  {
 2         mMediaPlayer.reset();到Idle状态
 3          4         File file= 5         Log.i("TAG",1)"> 7             Log.i("TAG",1)"> 8         } 11         Uri uri=13             mMediaPlayer.setDataSource(            mMediaPlayer.setDisplay(mSurfaceView.getHolder());
            mMediaPlayer.prepare();
            mMediaPlayer.start();
17         } 20     }
 bn_pause(View view){
 2         (mMediaPlayer.isPlaying()){
 bn_stop(View view){
11     }
 2      8             mMediaPlayer=11     }
1     uses-permission ="android.permission.INTERNET"/>
="android.permission.WRITE_EXTERNAL_STORAGE"3     ="android.permission.READ_EXTERNAL_STORAGE"4     ="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_67873.html