2017-06-12 73 views
-1

無法在棒棒糖版本的對話框中顯示MediaController。MediaController沒有與VideoView綁定,它在對話框中顯示在棒棒糖的Videoview後面,?

代碼表明以下棒棒糖而不是從棒棒糖的MediaController。

private void showMediacontroller(){ 

    View view =getLayoutInflater().inflate(R.layout.layout_viedo_dialog,null); 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setView(view); 
    AlertDialog alertDialog = builder.create(); 
    videoview = (VideoView) view.findViewById(R.id.videoView); 
    final MediaController mediaController = new MediaController(this); 
    mediaController.setAnchorView(videoview); 
    // Set MediaController for VideoView 
    videoview.setMediaController(mediaController); 

    Log.i("Play", "is showing-->"+mediaController.isShowing()); 

    Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sample); 
    videoview.setVideoURI(uri); 
    videoview.requestFocus(); 

    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() { 
     @Override 
     public void onShow(DialogInterface dialog) { 
      videoview.start(); 
      mediaController.show(); 
      Log.i("Play", "is showing-->"+mediaController.isShowing()); 
     } 
    }); 
    alertDialog.show(); 
} 

回答

0

最後我解決了這是的MediaController問題顯示videoview後面?

我把framelayout放在videoview的下方,並用 將framelayout錨起來。

2

公共類AudioRecorderActivity擴展BaseActivity {

private static final String TAG = "AudioRecorderActivity"; 
private static final int REQUEST_CODE = 1000; 
private int mScreenDensity; 
private MediaProjectionManager mProjectionManager; 
private static final int DISPLAY_WIDTH = 720; 
private static final int DISPLAY_HEIGHT = 1280; 
private MediaProjection mMediaProjection; 
private VirtualDisplay mVirtualDisplay; 
private MediaProjectionCallback mMediaProjectionCallback; 
private ToggleButton mToggleButton; 
private MediaRecorder mMediaRecorder; 
private static final SparseIntArray ORIENTATIONS = new SparseIntArray(); 
private static final int REQUEST_PERMISSIONS = 10; 

static { 
    ORIENTATIONS.append(Surface.ROTATION_0, 90); 
    ORIENTATIONS.append(Surface.ROTATION_90, 0); 
    ORIENTATIONS.append(Surface.ROTATION_180, 270); 
    ORIENTATIONS.append(Surface.ROTATION_270, 180); 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layout_audio_recording); 
    DisplayMetrics metrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(metrics); 
    mScreenDensity = metrics.densityDpi; 

    mMediaRecorder = new MediaRecorder(); 

    mProjectionManager = (MediaProjectionManager) getSystemService 
      (Context.MEDIA_PROJECTION_SERVICE); 

    mToggleButton = (ToggleButton) findViewById(R.id.toggle); 
    mToggleButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (ContextCompat.checkSelfPermission(this, 
        Manifest.permission.WRITE_EXTERNAL_STORAGE) + ContextCompat 
        .checkSelfPermission(this, 
          Manifest.permission.RECORD_AUDIO) 
        != PackageManager.PERMISSION_GRANTED) { 
       if (ActivityCompat.shouldShowRequestPermissionRationale 
         (this, Manifest.permission.WRITE_EXTERNAL_STORAGE) || 
         ActivityCompat.shouldShowRequestPermissionRationale 
           (this, Manifest.permission.RECORD_AUDIO)) { 
        mToggleButton.setChecked(false); 
        Snackbar.make(findViewById(android.R.id.content), R.string.label_permissions, 
          Snackbar.LENGTH_INDEFINITE).setAction("ENABLE", 
          new View.OnClickListener() { 
           @Override 
           public void onClick(View v) { 
            ActivityCompat.requestPermissions(this, 
              new String[]{Manifest.permission 
                .WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO}, 
              REQUEST_PERMISSIONS); 
           } 
          }).show(); 
       } else { 
        ActivityCompat.requestPermissions(this, 
          new String[]{Manifest.permission 
            .WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO}, 
          REQUEST_PERMISSIONS); 
       } 
      } else { 
       onToggleScreenShare(v); 
      } 
     } 
    }); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode != REQUEST_CODE) { 
     Log.e(TAG, "Unknown request code: " + requestCode); 
     return; 
    } 
    if (resultCode != RESULT_OK) { 
     Toast.makeText(this, 
       " Permission Denied", Toast.LENGTH_SHORT).show(); 
     mToggleButton.setChecked(false); 
     return; 
    } 
    mMediaProjectionCallback = new MediaProjectionCallback(); 
    mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data); 
    mMediaProjection.registerCallback(mMediaProjectionCallback, null); 
    mVirtualDisplay = createVirtualDisplay(); 
    mMediaRecorder.start(); 
} 

public void onToggleScreenShare(View view) { 
    if (((ToggleButton) view).isChecked()) { 
     initRecorder(); 
     shareScreen(); 
    } else { 
     mMediaRecorder.stop(); 
     mMediaRecorder.reset(); 
     Log.v(TAG, "Stopping Recording"); 
     stopScreenSharing(); 
    } 
} 

private void shareScreen() { 
    if (mMediaProjection == null) { 
     startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE); 
     return; 
    } 
    mVirtualDisplay = createVirtualDisplay(); 
    mMediaRecorder.start(); 
} 

private VirtualDisplay createVirtualDisplay() { 
    return mMediaProjection.createVirtualDisplay("AudioRecorderActivity", 
      DISPLAY_WIDTH, DISPLAY_HEIGHT, mScreenDensity, 
      DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, 
      mMediaRecorder.getSurface(), null /*Callbacks*/, null 
      /*Handler*/); 
} 

private void initRecorder() { 
    try { 
     mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); 
     mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
     mMediaRecorder.setOutputFile(Environment 
       .getExternalStoragePublicDirectory(Environment 
         .DIRECTORY_DOWNLOADS) + "/video.mp4"); 
     mMediaRecorder.setVideoSize(DISPLAY_WIDTH, DISPLAY_HEIGHT); 
     mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); 
     mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
     mMediaRecorder.setVideoEncodingBitRate(512 * 1000); 
     mMediaRecorder.setVideoFrameRate(30); 
     int rotation = getWindowManager().getDefaultDisplay().getRotation(); 
     int orientation = ORIENTATIONS.get(rotation + 90); 
     mMediaRecorder.setOrientationHint(orientation); 
     mMediaRecorder.prepare(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private class MediaProjectionCallback extends MediaProjection.Callback { 
    @Override 
    public void onStop() { 
     if (mToggleButton.isChecked()) { 
      mToggleButton.setChecked(false); 
      mMediaRecorder.stop(); 
      mMediaRecorder.reset(); 
      Log.v(TAG, "Recording Stopped"); 
     } 
     mMediaProjection = null; 
     stopScreenSharing(); 
    } 
} 

private void stopScreenSharing() { 
    if (mVirtualDisplay == null) { 
     return; 
    } 
    mVirtualDisplay.release(); 
    //mMediaRecorder.release(); //If used: mMediaRecorder object cannot 
    // be reused again 
    destroyMediaProjection(); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    destroyMediaProjection(); 
} 

private void destroyMediaProjection() { 
    if (mMediaProjection != null) { 
     mMediaProjection.unregisterCallback(mMediaProjectionCallback); 
     mMediaProjection.stop(); 
     mMediaProjection = null; 
    } 
    Log.i(TAG, "MediaProjection Stopped"); 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, 
             @NonNull String permissions[], 
             @NonNull int[] grantResults) { 
    switch (requestCode) { 
     case REQUEST_PERMISSIONS: { 
      if ((grantResults.length > 0) && (grantResults[0] + 
        grantResults[1]) == PackageManager.PERMISSION_GRANTED) { 
       onToggleScreenShare(mToggleButton); 
      } else { 
       mToggleButton.setChecked(false); 
       Snackbar.make(findViewById(android.R.id.content), R.string.label_permissions, 
         Snackbar.LENGTH_INDEFINITE).setAction("ENABLE", 
         new View.OnClickListener() { 
          @Override 
          public void onClick(View v) { 
           Intent intent = new Intent(); 
           intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 
           intent.addCategory(Intent.CATEGORY_DEFAULT); 
           intent.setData(Uri.parse("package:" + getPackageName())); 
           intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
           intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
           intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 
           startActivity(intent); 
          } 
         }).show(); 
      } 
      return; 
     } 
    } 
} 

}

相關問題