Androd 6.0 surfaceview 繼承parceable 接口能夠跨進程傳遞,如今咱們的從最簡單的單實例開始 java
下面是客戶端進程,主要是得到服務段的IBinder ,並在客戶端調用服務端Binder 實現的方法,完成進程間的通訊。 android
@SuppressLint("NewApi")} app
定義進程間通訊接口。 ide
IMySevice.aidl: this
package com.example.demo;
import android.view.Surface;
interface IMyService {
void playLoadedVideo_iadl(in Surface surface);
} spa
service 以及binder 實現。 xml
MyRemoteService.java: 繼承
package com.example.demo;
import java.io.IOException;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.Surface;
public class MyRemoteService extends Service {
private static final String LOG_TAG = MyRemoteService.class.getName() + "";
private Surface surface;
@SuppressLint("NewApi")
private final IMyService.Stub mBinder = new IMyService.Stub(){
@Override
public void playLoadedVideo_iadl(Surface surface)
throws RemoteException {
// TODO Auto-generated method stub
final Integer width = 1280;
final Integer height = 720;
int playTime = 0 ;
final float leftVolume = 0.5f;
final float rightVolume = 0.5f;
final MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(Constants.getFullPath("", ""));
} catch (IllegalArgumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if( surface != null) {
Log.i(LOG_TAG,surface.toString());
mediaPlayer.setSurface(surface);
}else {
Log.w(LOG_TAG,"surface is NULL");
}
//mediaPlayer.setScreenOnWhilePlaying(true);
mediaPlayer
.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
@Override
public void onVideoSizeChanged(MediaPlayer mp, int w, int h) {
if (w == 0 && h == 0) {
// A size of 0x0 can be sent initially one time when
// using NuPlayer.
// assertFalse(mOnVideoSizeChangedCalled.isSignalled());
return;
}
// mOnVideoSizeChangedCalled.signal();
if (width != null) {
}
if (height != null) {
}
}
});
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return true;
}
});
mediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
// mOnVideoRenderingStartCalled.signal();
}
return true;
}
});
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer arg0) {
// TODO Auto-generated method stub
mediaPlayer.start();
mediaPlayer.setVolume(leftVolume, rightVolume);
}
});
try {
mediaPlayer.prepareAsync();
} catch (Exception e) {
Log.w(LOG_TAG,e.toString());
mediaPlayer.reset();
}
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return mBinder;
}
} 接口
配置遠程服務獨立進程 進程
AndroidManifest.xml:
<activity
android:name="com.example.demo.MediaPlayer_Surface_iadl_TestBase"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:label="Media_Surface_StubActivity"
android:screenOrientation="nosensor" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.FRAMEWORK_INSTRUMENTATION_TEST" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyRemoteService"
android:process=":remote" > <intent-filter> <action android:name="com.example.demo.IMyService" /> </intent-filter> </service>