xiaxiao_new 2017-10-10 09:55 采纳率: 0%
浏览 1063

android 高分辨率设备上播放低分辨率电视直播源 怎么让视频左右不黑边

我用的是JCVideoplayer来播放视频源,我重写onMeasure发现视频窗口没了。现在我用的
是scaleX 强行把X轴变拉长,但是这样会造成其他控件变形
private JCVideoPlayerStandard jcVideoPlayerStandard;
jcVideoPlayerStandard = findViewById(R.id.video_player);
jcVideoPlayerStandard.setUp(mediaModel.getVideoUrl()
, JCVideoPlayerStandard.SCREEN_WINDOW_FULLSCREEN, mediaModel.getTitle());
jcVideoPlayerStandard.startPlayLocic();

            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
    android:id="@+id/video_player"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    />

<TextView
    android:id="@+id/tv_channel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_margin="40dp"
    android:text="15"
    android:textColor="@color/detail_background"
    android:textSize="60sp" />

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-07 00:16
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    在Android中,如果你想要在高分辨率的设备上播放低分辨率的电视直播源,并且希望保持视频的正确比例和边缘清晰度,你可以考虑使用一个专门针对流媒体内容优化的框架,例如MediaCodecAudioDecoder或者MediaCodecVideoDecoder。

    这些库提供了一种方法来解码流媒体数据并将其转换为支持Android平台的格式,同时可以处理诸如帧率、分辨率缩放等高级特性。

    以下是一个简单的示例,展示了如何使用MediaCodecVideoDecoder进行视频解码:

    import android.media.MediaCodec;
    import android.media.MediaCodecInfo;
    import android.media.MediaFormat;
    import android.media.MediaMuxer;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Surface;
    import androidx.appcompat.app.AppCompatActivity;
    import java.io.IOException;
    import org.opencv.core.Core;
    import org.opencv.core.Mat;
    import org.opencv.imgcodecs.Imgcodecs;
    
    public class MainActivity extends AppCompatActivity {
    
        private static final String TAG = "MainActivity";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            MediaFormat format = MediaFormat.createVideoFormat("video/VP8", 320, 240); // Set the video format
            MediaCodec codec = new MediaCodec(); // Create a MediaCodec instance
            codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); // Configure the codec for encoding
            MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
    
            while (true) {
                int index = codec.dequeueInputBuffer(5000); // Dequeue input buffer
                if (index >= 0) {
                    Mat src = Imgcodecs.imread("/path/to/source.jpg"); // Load the source image
                    Mat dst = new Mat(src.size(), src.type()); // Allocate memory for the output frame
                    Core.matToMat(dst, src); // Copy the source image to the output frame
    
                    codec.queueOutputBuffer(info, true); // Queue output buffer
                    codec.release(); // Release the codec
                    break; // Exit loop
                }
            }
    
            try {
                while (!codec.isStopped()) {
                    MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
                    int result = codec.decodeSampledFrame(bufferInfo, 0, null);
                    if (result == MediaCodec.INFO_TRY_AGAIN_LATER) {
                        Thread.sleep(1000);
                        continue;
                    } else if (result == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
                        MediaFormat newFormat = codec.getOutputFormat();
                        MediaFormat newFormat2 = MediaFormat.createVideoFormat(newFormat.getString(MediaFormat.KEY_MIME), 320, 240); // Set the video format again
                        codec.configure(newFormat2, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); // Reconfigure the codec for encoding
                        codec.setOutputFormat(newFormat2);
                    }
                    if (bufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM != 0) { // Check if we have reached the end of the stream
                        break;
                    }
                    // Process the decoded data...
                }
            } catch (IOException e) {
                Log.e(TAG, "Error decoding video", e);
            } finally {
                codec.stop();
                codec.release();
            }
        }
    }
    

    这个示例首先创建了一个MediaCodec实例,然后配置它以编码视频。然后,它循环读取输入缓冲区的数据,解码输入缓冲区中的视频数据,将输出缓冲区填充到内存中,然后释放输入缓冲区。最后,当解码完成时,它会停止编码器并释放它。

    注意:这只是一个基本的示例,实际的应用可能需要更复杂的错误处理和性能优化。

    评论

报告相同问题?