اذهب إلى المحتوى
  • 0

اريد ضغط ملف الفيديو قبل رفعه على السيرفر ولكن لدى مشكلة ولا اعلم اين هى فى الكود

Drive Man

السؤال

public class uploadved extends AppCompatActivity implements difroved.ExampleDialogeListener{
    TextView uploader;
    RoundedImageView geter;
    VideoView vedgetr;
    MediaController mc;
    File file;
   private static final int SELECT_VIDEO_REQUEST = 0;
    public Uri videoUri;
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uploadved);

        geter = findViewById(R.id.geter);
        uploader = findViewById(R.id.uploader);
        vedgetr = findViewById(R.id.vedgetr);

        mc = new MediaController(uploadved.this);
        vedgetr.setMediaController(mc);

        geter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    selectVideo();

            }
        });

    private void selectVideo() {
        Intent intent = new Intent();
        intent.setType("video/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Video"), SELECT_VIDEO_REQUEST);

    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == SELECT_VIDEO_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            videoUri = data.getData();
            vedgetr.setVideoURI(videoUri);
            vedgetr.start();
            file=new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath());

            float size1=file.length()/1024;
            long size = file.length();

            MediaPlayer mp = MediaPlayer.create(this, videoUri);

           new  Compressvedio().execute("false",videoUri.toString(),file.getPath());

            int duration = mp.getDuration();
            mp.release();
            String durationStr = String.format("%d", TimeUnit.MILLISECONDS.toSeconds(duration) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration)));
            String durationStr2 = String.format("%d", TimeUnit.MILLISECONDS.toMinutes(duration));


        }

        }
   /////////////////////////on COMPROSSER//////////////


    private class Compressvedio extends AsyncTask<String, String, String> {
        Dialog dialogo;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialogo = ProgressDialog.show(uploadved.this, "برجاء الانتظار", "جارى تجهيز الفيديو");
        }

        @Override
        protected String doInBackground(String... strings) {
            String vediopath = null;

            // Get the video URI.
            Uri uritoup = Uri.parse(strings[1]);

            // Compress the video.
            try {
                vediopath = SiliCompressor.with(uploadved.this).compressVideo(uritoup, strings[2]);
            } catch (URISyntaxException e) {
                throw new RuntimeException(e);
            }

            return vediopath;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            dialogo.dismiss();

            // Get the compressed video file.
            File filea = new File(s);

            // Get the file size in kilobytes.
            float size2 = filea.length() / 1024f;

            // Log the file size.
            Log.d("sizs", String.format("Size : %.2f kB", size2));

            // Set the display name of the compressed video file.
            displayName = String.valueOf(Calendar.getInstance().getTimeInMillis() + ".mp4");
        }
    }

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

تحتاج إلى تنفيذ طريقة onPostExecute في Compressvedio لتنفيذ الإجراءات المطلوبة بعد الانتهاء من ضغط الفيديوK ,إضافة رمز الإرسال إلى السيرفر في تلك الطريقة.

تحتاج أيضًا إلى تمرير المسار الذي تم ضغطه للدالة Compressvedio().execute() في onActivityResult()، وأنت تمرر file.getPath() ولكن ذلك غير صحيح، فيجب عليك تمرير مسار الملف الذي تم ضغطه من خلال vediopath في doInBackground().

أي في onActivityResult(): قم بتعيين vediopath إلى القيمة المسترجعة من Compressvedio().execute() بدلاً من file.getPath().

قبل التعديل:

new Compressvedio().execute("false", videoUri.toString(), file.getPath());

بعد التعديل:

new Compressvedio().execute("false", videoUri.toString(), vediopath);

وفي Compressvedio، قم بتنفيذ الإجراءات اللازمة بعد الانتهاء من ضغط الفيديو داخل onPostExecute().

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
    dialogo.dismiss();

    // Get the compressed video file.
    File filea = new File(s);

    // Get the file size in kilobytes.
    float size2 = filea.length() / 1024f;

    // Log the file size.
    Log.d("sizs", String.format("Size : %.2f kB", size2));

    // Set the display name of the compressed video file.
    displayName = String.valueOf(Calendar.getInstance().getTimeInMillis() + ".mp4");

    // Perform further actions here, such as uploading the compressed video to the server.
}

وتأكد من إضافة الكود اللازم لرفع الملف المضغوط إلى السيرفر في المكان المناسب داخل onPostExecute().

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

الرمز الذي قدمته يبدو أنه يحاول ضغط ملف فيديو قبل تحميله إلى الخادم. ومع ذلك، هناك مشكلة في التعليمات البرمجية في فئة Compressvedio. لا تُرجع طريقة doInBackground() مسار ملف الفيديو المضغوط. هذا يعني أن طريقة onPostExecute() لن تتمكن من الحصول على ملف الفيديو المضغوط وتسجيل حجمه.

لإصلاح هذه المشكلة، تحتاج إلى تعديل طريقة doInBackground() لإرجاع مسار ملف الفيديو المضغوط. يمكنك القيام بذلك عن طريق إضافة التعليمات البرمجية التالية إلى الطريقة:

String vediopath = SiliCompressor.with(uploadved.this).compressVideo(uritoup, strings[2]); return vediopath;

بمجرد إجراء هذه التغييرات، يجب أن يكون الرمز قادرًا على ضغط ملف الفيديو وتسجيل حجمه.

إليك الكود الكامل لفئة Compressvedio:

public class Compressvedio extends AsyncTask<String, String, String> {
Dialog dialogo;

@Override
protected void onPreExecute() {
    super.onPreExecute();
    dialogo = ProgressDialog.show(uploadved.this, "برجاء الانتظار", "جارى تجهيز الفيديو");
}

@Override
protected String doInBackground(String... strings) {
    String vediopath = null;

    // Get the video URI.
    Uri uritoup = Uri.parse(strings[1]);

    // Compress the video.
    try {
        vediopath = SiliCompressor.with(uploadved.this).compressVideo(uritoup, strings[2]);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }

    return vediopath;
}

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
    dialogo.dismiss();

    // Get the compressed video file.
    File filea = new File(s);

    // Get the file size in kilobytes.
    float size2 = filea.length() / 1024f;

    // Log the file size.
    Log.d("sizs", String.format("Size : %.2f kB", size2));

    // Set the display name of the compressed video file.
    displayName = String.valueOf(Calendar.getInstance().getTimeInMillis() + ".mp4");
}
}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

يبدو أن لديك بعض المشاكل في كودك. هنا بعض الأمور التي يجب أن تلاحظها:

  1. يجب عليك إغلاق الأقواس الناقصة في طريقة onCreate() الخاصة بك. يجب إضافة السطر } قبل طريقة selectVideo().
  2. قمت بتعريف متغير file كـ File جديد، ولكن لم تقم بتعيين مسار الملف. يبدو أنه يجب تعيين مسار الملف المستهدف في هذا المتغير قبل استخدامه في طريقة Compressvedio.
  3. يجب تمرير المعلمات الصحيحة إلى طريقة Compressvedio. يجب أن يكون لديك 3 معاملات: strings[0] يحتوي على القيمة "false"، strings[1] يحتوي على سلسلة URI لمقطع الفيديو، و strings[2] يحتوي على مسار الملف الذي سيتم حفظ المقطع المضغوط فيه. تأكد من تمرير هذه المعاملات بالترتيب الصحيح في الدالة execute().
  4. في طريقة Compressvedio.onPostExecute() ، يجب استخدام المتغير size2 للإشارة إلى الحجم بدلاً من المتغير size1.

بعد أن تقوم بإصلاح هذه المشاكل المحددة، قد تستمر في وجود مشاكل أخرى. في حال حدوث أي مشكلة إضافية، يرجى تقديم معلومات إضافية حول الخطأ الذي تواجهه وأية رسائل خطأ تظهر في وحدة التحكم.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0
بتاريخ 5 دقائق مضت قال Drive Man:

هل يمكنك اعطائى الكود كامل 

تفضل هذا الكود بعد حل المشكلات المذكوره سابقا


تمت إضافة getFilePathFromUri() كدالة مساعدة للحصول على مسار الملف من URI المستخدمة في selectVideo(). هذا يساعد في الحصول على المسار الصحيح للملف الذي تم تحديده من قبل المستخدم.

أيضًا، تم إضافة تعليقات توضيحية لأجزاء محددة من الكود. يمكنك استخدام هذا الكود كنقطة بداية لإجراء التعديلات اللازمة حسب احتياجاتك.

public class UploadVideoActivity extends AppCompatActivity implements difroved.ExampleDialogeListener {
    TextView uploader;
    RoundedImageView geter;
    VideoView vedgetr;
    MediaController mc;
    File file;
    private static final int SELECT_VIDEO_REQUEST = 0;
    public Uri videoUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uploadved);

        geter = findViewById(R.id.geter);
        uploader = findViewById(R.id.uploader);
        vedgetr = findViewById(R.id.vedgetr);

        mc = new MediaController(uploadved.this);
        vedgetr.setMediaController(mc);

        geter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectVideo();
            }
        });
    }

    private void selectVideo() {
        Intent intent = new Intent();
        intent.setType("video/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Video"), SELECT_VIDEO_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == SELECT_VIDEO_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            videoUri = data.getData();
            vedgetr.setVideoURI(videoUri);
            vedgetr.start();

            // Get the file path from the video URI
            String filePath = getFilePathFromUri(videoUri);

            file = new File(filePath);

            float size1 = file.length() / 1024f;
            long size = file.length();

            MediaPlayer mp = MediaPlayer.create(this, videoUri);

            // Compress the video in the background
            new CompressVideoTask().execute("false", videoUri.toString(), file.getPath());

            int duration = mp.getDuration();
            mp.release();
            String durationStr = String.format("%d", TimeUnit.MILLISECONDS.toSeconds(duration) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration)));
            String durationStr2 = String.format("%d", TimeUnit.MILLISECONDS.toMinutes(duration));
        }
    }

    // Helper method to get the file path from the URI
    private String getFilePathFromUri(Uri uri) {
        String filePath = "";
        String[] projection = {MediaStore.Video.Media.DATA};
        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            filePath = cursor.getString(columnIndex);
            cursor.close();
        }
        return filePath;
    }

    private class CompressVideoTask extends AsyncTask<String, String, String> {
        Dialog dialogo;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialogo = ProgressDialog.show(uploadved.this, "برجاء الانتظار", "جاري تجهيز الفيديو");
        }

        @Override
        protected String doInBackground(String... strings) {
            String vediopath = null;

            // Get the video URI.
            Uri uritoup = Uri.parse(strings[1]);

            // Compress the video.
            try {
                vediopath = SiliCompressor.with(uploadved.this).compressVideo(uritoup, strings[2]);
            } catch (URISyntaxException e) {
                throw new RuntimeException(e);
            }

            return vediopath;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            dialogo.dismiss();

            // Get the compressed video file.
            File filea = new File(s);

            // Get the file size in kilobytes.
            float size2 = filea.length() / 1024f;

            // Log the file size.
            Log.d("sizs", String.format("Size : %.2f kB", size2));

            // Set the display name of the compressed video file.
            String displayName = String.valueOf(Calendar.getInstance().getTimeInMillis() + ".mp4");
        }
    }
}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...