zoukankan      html  css  js  c++  java
  • 6.1.2 加上MediaPlayer的本地服务

        既然已经创建了一个服务示例,现在可以将它作为模板来创建应用程序,以实现在后台播放音频文件。下面是一个服务以及一个控制服务的活动,这就使我们能够在后台播放音频文件。它的工作方式与第5章中自定义的音频播放器示例类似,因为它使用Android提供的相同的底层MediaPlayer类。

    1 package com.nthm.androidtestService;
    2 
    3 import com.nthm.androidtest.R;
    4 import android.app.Service;
    5 import android.content.Intent;
    6 import android.media.MediaPlayer;
    7 import android.media.MediaPlayer.OnCompletionListener;
    8 import android.os.IBinder;

        该服务实现了OnCompletionListener,因此在MediaPlayer完成播放音频文件时可以得到通知。

    1 public class BackgroundAudioService extends Service implements
    2         OnCompletionListener {

        声明一个MediaPlayer类型的对象。该对象将处理音频的播放,与第5章中自定义的音频播放器示例一样。

    1     private MediaPlayer mediaPlayer;
    2     @Override
    3     public IBinder onBind(Intent intent) {
    4         return null;
    5     }
    6     @Override
    7     public void onCreate() {
    8         super.onCreate();

        在onCreate方法中实例化该MediaPlayer。把一个名为music.mp3的音频文件的具体引用传递给他,该文件应该放在项目的原始资源目录(res/raw)中。若该目录不存在,则创建它。将音频文件放在该位置,使得我们能够通过生成的R类R.raw.music中的常量来引用它。关于把音频文件放在原始资源目录中的更详细信息,可参看5.1.3节。

    1         mediaPlayer=MediaPlayer.create(this, R.raw.music);

         同时将当前服务(即this)设置为MediaPlayer对象的OnCompletionListener。

    1         mediaPlayer.setOnCompletionListener(this);
    2     }

        当在该服务上发出startService命令时,将触发onStartCommand方法。由于可能多次调用该方法,因此这个方法首先将检查MediaPlayer对象是否已在播放,若没有播放,则启动它。由于使用了onStartCommand方法而非onStart方法,因此该示例仅能在Android 2.0及以上版本中运行。

    1     @Override
    2     public int onStartCommand(Intent intent, int flags, int startId) {
    3         if(!mediaPlayer.isPlaying()){
    4             mediaPlayer.start();
    5         }
    6         return START_STICKY;
    7     }

       当服务销毁时,触发onDestroy方法。由于这并不能确保MediaPlayer会停止播放,因此如果它还在播放,那么在这里将调用它的stop方法,同时还会调用其release方法来清除内存使用和(或)资源锁。

    1     @Override
    2     public void onDestroy() {
    3         if(mediaPlayer.isPlaying()){
    4             mediaPlayer.stop();
    5         }
    6         mediaPlayer.release();
    7         super.onDestroy();
    8     }

       由于实现了OnCompletionListener,且服务本身被设置为MediaPlayer对象的OnCompletionListener,因此当MediaPlayer完成播放音频文件时,将调用如下的onCompletion方法。由于这个服务只用于播放一首歌,因此将调用stopSelf方法,其类似于在活动中调用stopService方法。

    1     @Override
    2     public void onCompletion(MediaPlayer mp) {
    3          stopSelf();
    4     }
    5 }

       以下是对应上述服务的活动。它有一个非常简单的界面,使用两个按钮来控制服务的启动和停止。在每种情况下,它在后面都会直接调用finish方法,以表明该服务不依赖于活动而独立运行。

     1 package com.nthm.androidtestActivity;
     2 
     3 import com.nthm.androidtest.R;
     4 import com.nthm.androidtestService.BackgroundAudioService;
     5 import android.app.Activity;
     6 import android.content.Intent;
     7 import android.os.Bundle;
     8 import android.view.View;
     9 import android.view.View.OnClickListener;
    10 import android.widget.Button;
    11 
    12 public class BackgroundAudioActivity extends Activity implements
    13         OnClickListener {
    14     private Button startPlaybackButton;
    15     private Button stopPlaybackButton;
    16     private Intent playbackServiceIntent;
    17     @Override
    18     protected void onCreate(Bundle savedInstanceState) {
    19         super.onCreate(savedInstanceState);
    20         setContentView(R.layout.backgroundaudioactivity);
    21         startPlaybackButton=(Button) findViewById(R.id.StartPlayerbackButton);
    22         stopPlaybackButton=(Button) findViewById(R.id.StopPlayerbackButton);
    23         startPlaybackButton.setOnClickListener(this);
    24         stopPlaybackButton.setOnClickListener(this);
    25         playbackServiceIntent=new Intent(this, BackgroundAudioService.class);
    26     }
    27 
    28     @Override
    29     public void onClick(View v) {
    30       if(v==startPlaybackButton){
    31           startService(playbackServiceIntent);
    32           finish();
    33       }else if(v==stopPlaybackButton){
    34           stopService(playbackServiceIntent);
    35           finish();
    36       }
    37     }
    38 }

        下面是上述活动使用的backgroundaudioactivity.xml布局的XML文件。

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:layout_width="match_parent"
     3     android:layout_height="match_parent"
     4     android:orientation="vertical"
     5     >
     6  <TextView 
     7      android:text="Backgroung Audio Player"
     8      android:layout_width="fill_parent"
     9      android:layout_height="wrap_content"></TextView>
    10  <Button 
    11      android:layout_width="wrap_content"
    12      android:layout_height="wrap_content"
    13      android:id="@+id/StartPlayerbackButton"
    14      android:text="Start Playerback"/>
    15  <Button 
    16      android:layout_width="wrap_content"
    17      android:layout_height="wrap_content"
    18      android:id="@+id/StopPlayerbackButton"
    19      android:text="Stop Playerback"/>
    20 </LinearLayout>

        如上面的代码所示,在服务中仅仅使用MediaPlayer来启动和停止音频播放非常的简单。现在让我们看看如何更深入一步。

       

  • 相关阅读:
    JavaScript操作符instanceof揭秘
    Linux打开txt文件乱码的解决方法
    Working copy locked run svn cleanup not work
    poj 2299 UltraQuickSort 归并排序求解逆序对
    poj 2312 Battle City 优先队列+bfs 或 记忆化广搜
    poj2352 stars 树状数组
    poj 2286 The Rotation Game 迭代加深
    hdu 1800 Flying to the Mars
    poj 3038 Children of the Candy Corn bfs dfs
    hdu 1983 Kaitou Kid The Phantom Thief (2) DFS + BFS
  • 原文地址:https://www.cnblogs.com/ZSS-Android/p/3941699.html
Copyright © 2011-2022 走看看